<?php

namespace Seller\Controller;
use Think\Controller;
class IndexController extends Controller 
{
    public function __construct()  
    {  
        parent::__construct();  
        $res = isLogin();
        if($res == '1')
        {
			$this->redirect('/Seller/Login/index');
            //$this -> error('还未登录',__APP__.'/Seller/Login/index');
        }
        
        $arr['shop_id'] = session('shop_id');
        $shopModel = D('Shop');
        $res = $shopModel->points($arr['shop_id']);
    } 

    public function index()
    {
        //积分进出记录
        $pay['shop_id'] = session('shop_id');
        $pay['is_user'] = 0;
		$payModel = M('recharge');
        
        $count      = $payModel->where($pay)->count();

        $arr['shop_id'] = session('shop_id');
        $shopModel = D('Shop');
        $res = $shopModel->shopUser($arr['shop_id']);
        //var_dump($res);exit();
        $this->assign('count', $count);
        $this->assign('res', $res);
        $this->display();
    }

    /*退出*/
    public function seigOut()
    {
    	session('shop_id', 0);
    	session('openid', 0);
		session_destroy();
        $this->success('已退出登录！', __APP__.'/Seller/Login/index');
    }

    /*信息修改*/
    public function change()
    {
        //积分进出记录
        $pay['shop_id'] = session('shop_id');
        $pay['is_user'] = 0;
        $payModel = M('recharge');
        $count      = $payModel->where($pay)->count();

        $arr['shop_id'] = session('shop_id');
        
        $shopModel = D('Shop');
        $res = $shopModel->shopUser($arr['shop_id']);

        $areaModel = D('Area');
        $areaRes = $areaModel -> index('area');

        $classModel = D('Class');
        $classRes = $classModel -> index();

        $this->assign('count', $count);
        $this->assign('areaRes', $areaRes);
        $this->assign('classRes', $classRes);
        $this->assign('res', $res);
        $this->display();
    }

    public function changed()
    {	
		if(I('post.old_pwd') && I('post.new_pwd')){
			$arr['old_pwd']   = md5(I('post.old_pwd'));
			$arr['pwd']   = md5(I('post.new_pwd'));			
		}
        $arr['shop_id']   = trim(I('post.id'));
        $arr['contacts']   = trim(I('post.contacts'));

        $arr['area'] = trim(I('post.area'));
        $arr['type']   = trim(I('post.type'));
        $arr['address'] = trim(I('post.address'));
        $arr['synopsis']   = trim(I('post.synopsis'));
        $arr['card'] = trim(I('post.card'));
        $arr['banktype']   = trim(I('post.banktype'));
        $arr['realname']   = trim(I('post.realname'));
        $arr['idcard']   = trim(I('post.idcard'));
        $arr['openbank']   = trim(I('post.openbank'));

		$ophoto1 = trim(I('post.o_photo1'));
		
		
		$ophoto2 = trim(I('post.o_photo2'));

					
        $shopModel = D('shop');
        $path = $shopModel->upload();
		
        if($path)
        {	
			if($path['photo1']){
				 $arr['photo1']   = $path['photo1']['savepath'].$path['photo1']['savename'];
			}else{
				$arr['photo1'] = $ophoto1;
			}
			if($path['photo2']){
				 $arr['photo2']   = $path['photo2']['savepath'].$path['photo2']['savename'];
			}else{
				$arr['photo2'] = $ophoto2;
			}			
                     
        }else
        {
            //$this -> error('图片保存失败', 'change');
        }

        $res = $shopModel->changed($arr);

        if($res == 'not') 
        {
        	$this -> error('旧密码填写错误', 'change');
        }elseif($res == 'success')
        {
        	$this->success('信息修改成功');
        }elseif($res == 'err')
        {
        	$this -> error('信息修改失败', 'change');
        }
        
    }

    /*赠送积分*/
    public function give()
    {
        //积分进出记录
        $pay['shop_id'] = session('shop_id');
        $pay['is_user'] = 0;
        $payModel = M('recharge');
        $count      = $payModel->where($pay)->count();
        $arr['shop_id'] = session('shop_id');
        $shopModel = D('Shop');
        $res = $shopModel->shopUser($arr['shop_id']);

        $this->assign('count', $count);
        $this->assign('res', $res);

        $this->display();
    }

    /*赠送*/
    public function giveUser()
    {
        $points['shop_id'] = session('shop_id');
        $points['phone']   = trim(I('post.phone'));
        $points['number']   = trim(I('post.number'));
        $points['reason']   = '消费积分';
		
		if($points['number'] <= 0){
			$this -> error('赠送积分金额必须大于0');
			exit();
		}
		
        $shopModel = D('Shop');
        $res = $shopModel->givePoints($points);

        if($res == 'notPhone')
        {
        	$this -> error('填写的电话格式不正确！');
        }elseif($res == 'notHas')
        {
        	$this -> error('此会员不存在！');
        }elseif($res == 'rechargeErr')
        {
        	$this -> error('商家充值失败');
        }elseif($res == 'logErr')
        {
        	$this -> error('会员充值失败！');
        }elseif($res == 'logSession')
        {
        	$this -> success('会员充值成功！');
        }elseif($res == 'noNumber')
        {
            $this -> success('余额不足！');
        }
    }

    /*商品上新*/
    public function goods()
    {
        //积分进出记录
        $pay['shop_id'] = session('shop_id');
        $pay['is_user'] = 0;
        $payModel = M('recharge');
        $count      = $payModel->where($pay)->count();

        $arr['shop_id'] = session('shop_id');
        $shopModel = D('Shop');
        $res = $shopModel->shopUser($arr['shop_id']);

        $areaModel = D('Area');
        $areaRes = $areaModel -> index('area');
        $classModel = D('Class');
        $classRes = $classModel -> index();

        $this->assign('count', $count);
        $this->assign('areaRes', $areaRes);
        $this->assign('classRes', $classRes);
        $this->assign('res', $res);
        $this->display();
    }

    public function goodsTo()
    {
        $goods['shop_id'] = session('shop_id');
        $goods['name']   = trim(I('post.name'));
        $goods['type']   = trim(I('post.type'));
        $goods['synopsis']   = trim(I('post.synopsis'));

        $shopModel = D('shop');
        $path = $shopModel->upload();

        if($path)
        {
            $goods['photo1']   = $path['photo1']['savepath'].$path['photo1']['savename'];
            $goods['photo2']   = $path['photo2']['savepath'].$path['photo2']['savename'];
        }else
        {
            $this -> error('图片保存失败', 'change');
        }

        $goodsModel = D('goods');
        $goodsRes = $goodsModel->goodsIn($goods);

        if($goodsRes == 'success')
        {
            $this -> success('商品上新成功！');
        }elseif($goodsRes == 'err')
        {
            $this -> error('商品上新失败！');
        }

    }


    /*购买积分*/
    public function buy()
    {
        //积分进出记录
        $pay['shop_id'] = session('shop_id');
        $pay['is_user'] = 0;
		$payModel = M('recharge');
        
        $count      = $payModel->where($pay)->count();

        $arr['shop_id'] = session('shop_id');
        $shopModel = D('Shop');
        $res = $shopModel->shopUser($arr['shop_id']);

        $this->assign('count', $count);
        $this->assign('res', $res);
        $this->display();

    }
	
	/**/
	public function new_way_wz(){
	/* 	$this -> assign('id',$_GET['id']); */
		$this -> display();
	}
	
	
	/**/
	public function new_way(){
/* 		$id = $_GET['id'];
		if($id == 1){
			$key = "50";
		}elseif($id == 2){
			$key = "100";
		}elseif($id == 3){
			$key = "500";
		}elseif($id == 4){
			$key = "1000";
		}elseif($id == 5){
			$key = "3000";
		}elseif($id == 6){
			$key = "5000";
		}
		$this -> assign('key',$key); */
		$this -> display();
	}
	
	public function my_alipay(){
		$pay = M("wxpay");
		$orderid =  date('YmdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
		$shopid = $_POST['id']; 
		$money = $_POST['num'] - 1.5;
		if($money <= 0){
			$this->error('充值金额不能小于0', 'Index/index');
			exit();
		}
		//存入支付数据
		$data = array(
			'orderid' => $orderid,
			'shopid' => $shopid,
			'money' => $money,
			'type' => 2,
			'status' => 1,
			'createtime' => time(),
		);
		$pay -> add($data);

		$this -> redirect('Index/to_alipay',array('url'=>$orderid));
	}
	
	public function to_alipay(){
		
		$url = "http://".$_SERVER['HTTP_HOST']."/alipay/index.php?order_sn=".$_GET['url'];
		
		$this -> assign('url',$url);
		$this -> display();
	}
	
	
    /*支付*/
    public function toBuy()
    {   
        $configModer = M('config');
        $configRes   = $configModer->select();
        $arr['buy'] = $configRes[0]['buy'];
        
        $arr['id'] = $_GET['id'];
        $arr['money'] = $_GET['money']/100;
        //var_dump($arr);exit();
        $payModel = D('Pay');
        $res = $payModel->toBuy($arr);
        if($res)
        {
            $this -> redirect('Index/give');
        }else
        {
            $this->error('积分充值有问题，请联系平台！', 'Index/buy');
        }

    }

    /*金额进出*/
    public function buyNum()
    {
        $arr['shop_id'] = session('shop_id');
        
        $rModel = M('recharge');
        
        $count      = $rModel->where($arr)->count();
        $Page       = new \Think\Page($count,1000);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $rModel->where($arr)->order('shijian desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        //var_dump($show);exit();
        $this->display();

    }

    //推荐商家二维码
    public function tjshop(){
        $n_url = 'http://'.$_SERVER['HTTP_HOST'].'/index.php/Seller/Login/register1/sid/'.session('shop_id');
        $url = "http://qr.liantu.com/api.php?text=".$n_url;
        $this -> assign('url',$url);
        $this -> display();
    }
    
    //推荐会员二维码
    public function tjcode(){
        $n_url = 'http://'.$_SERVER['HTTP_HOST'].'/index.php/Admin/Login/register1/sid/'.session('shop_id');
        $url = "http://qr.liantu.com/api.php?text=".$n_url;
        $this -> assign('url',$url);
        $this -> display();
    }
	
	/*我要提现*/
    public function tMoney()
    {
        $this->display();
    }
    /*发起提现申请*/
    public function cash()
    {
        $pay['shop_id'] = session('shop_id');
        $shop['phone']   = trim(I('post.phone'));
        $pay['number']   = trim(I('post.number'));

		if($pay['number'] < 100){
			$this->error('提现金额不能小于100');
			exit();			
		}
		
        $shopModel = M('shop');
        $shopRes = $shopModel->where($shop)->find();
		
		if(empty($shopRes['realname']) || empty($shopRes['idcard']) || empty($shopRes['openbank']) || empty($shopRes['card']) ){
			$this->error('请先完善个人提现信息',  __APP__.'/Seller/Index/change');
			exit();			
		}
		
		
		if($shopRes['use_points'] < $pay['number']){
			$this->error('提现金额不能大于可提现积分',  __APP__.'/Seller/Index/index');
			exit();
		}
		
        if($pay['shop_id'] == $shopRes['shop_id']){
            $pay['shop_id'] = session('shop_id');
            $pay['number']   = trim(I('post.number'));

            $payModer = D('Pay');
            $res       = $payModer->cash($pay);

            if($res == 'in')
            {
                $this->success('已成功发起申请！', __APP__.'/Seller/Index/index');
            }elseif ($res == 'no') 
            {
                $this->error('发起申请失败！',  __APP__.'/Seller/Index/index');
            }elseif ($res == 'noNum') 
            {
                $this->error('返还积分达到100才可提现！',  __APP__.'/Seller/Index/index');
            }
        }else{
            $this->error('店铺核实错误！',  __APP__.'/Seller/Index/index');
        }
    }
    
    /*我要提现
    public function tMoney()
    {
        $this->display();
    }*/
    /*发起提现申请
    public function cash()
    {
        $shop['phone'] = trim(I('post.phone'));
	    $shopModer = M('shop');
        $shopRes       = $shopModer->where($shop)->find();
		if($shopRes['shop_id'] == session('shop_id')){
			
			 $pay['shop_id'] = session('shop_id');
			$pay['number']   = trim(I('post.number'));
	
			$payModer = D('Pay');
			$res       = $payModer->cash($pay);
	
			if($res == 'in')
			{
				$this->success('已成功发起申请！', __APP__.'/Seller/Index/index');
			}elseif ($res == 'no') 
			{
				$this->error('发起申请失败！',  __APP__.'/Seller/Index/index');
			}elseif ($res == 'noNum') 
			{
				$this->error('返还积分达到100是才可提现！',  __APP__.'/Seller/Index/index');
			}
				
		}else{
			$this->error('非本人电话，不能进行提现！',  __APP__.'/Seller/Index/index');
		}
    }*/
    
}